package com.temp.controller.admin;

import com.temp.dto.LangDTO;
import com.temp.dto.SearchLangDTO;
import com.temp.entity.Language;
import com.temp.result.Result;
import com.temp.service.LanguageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/admin/language")
@Api(tags = "管理语言相关接口")
@Slf4j
@Validated
public class AdminLanguageController {

    @Autowired
    private LanguageService languageService;

    @PostMapping("/query")
    @ApiOperation("管理员获取所有语言")
    @RequiresPermissions("admin:language:query")
    public Result getAllLanguage(@RequestBody SearchLangDTO langDto) {
        langDto.formatParam();
        return Result.success(languageService.findAllLanguage(langDto),"apiMsg.querySuccess");
    }

    @PostMapping("/updateOrSave")
    @ApiOperation("保存或者更新语言")
    @RequiresPermissions("admin:language:updateOrSave")
    public Result updateOrSave(@Valid @RequestBody LangDTO langDTO) {
        Language language = new Language();
        BeanUtils.copyProperties(langDTO, language);
        boolean isSuccess = languageService.saveOrUpdate(language);

        return isSuccess ? Result.success("apiMsg.optionSuccess") : Result.error("apiMsg.optionError");
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除语言")
    @RequiresPermissions("admin:language:delete")
    public Result delCategory(@RequestParam Long id) {
        boolean isSuccess = languageService.removeById(id);

        return isSuccess ? Result.success("apiMsg.optionSuccess") : Result.error("apiMsg.optionError");
    }
}
